//zTree的配置
var setting = {
    view: {
        addHoverDom: addHoverDom,
        removeHoverDom: removeHoverDom,
        selectedMulti: false
    },
    edit: {
        enable: true,
        editNameSelectAll: true,
        showRemoveBtn: showRemoveBtn,
        showRenameBtn: showRenameBtn
    },
    data: {
        simpleData: {
            enable: true
        }
    },
    callback: {
        onClick:zTreeOnClick, //点击选中事件　　
        beforeDrag: beforeDrag,    // 可设置是否允许拖拽功能
        beforeEditName: beforeEditName,
        beforeRemove: beforeRemove,
        beforeRename: beforeRename,
        onRemove: onRemove,
        onRename: onRename
    }
};

//zTree初始化加载的节点树
var zNodes =[
    { id:1, pId:0, name:"问题类型管理", open:true},  //open属性的值true和false，表示是否在初始化加载后展开子节点
    { id:11, pId:1, name:"子节点 1-1"},
    { id:12, pId:1, name:"子节点 1-2"},
    { id:13, pId:1, name:"子节点 1-3"},
];

var log, className = "dark";
function zTreeOnClick(event, treeId, treeNode) {
    console.log('zTreeOnClick');
    console.log(treeNode)
    var treeObj = $.fn.zTree.getZTreeObj("treeDemo"),
        nodes = treeObj.getCheckedNodes(true),
        v = "";
    for (var i = 0; i < nodes.length; i++) {
        v += nodes[i].name + ",";
        console.log("节点id:" + nodes[i].id + "节点名称" + v); //获取选中节点的值
    }
}
function beforeDrag(treeId, treeNode) {
    return false; //false=禁用拖拽功能， true=开启
}
function beforeEditName(treeId, treeNode) {
    console.log('beforeEditName');
    console.log(treeNode)
    className = (className === "dark" ? "":"dark");
    var zTree = $.fn.zTree.getZTreeObj("treeDemo");
    zTree.selectNode(treeNode);
    zTree.editName(treeNode);
}
function beforeRemove(treeId, treeNode) {
    console.log('beforeRemove');
    className = (className === "dark" ? "":"dark");
    var zTree = $.fn.zTree.getZTreeObj("treeDemo");
    zTree.selectNode(treeNode);
    return confirm("确认删除 节点 -- " + treeNode.name + " 吗？");
}
function onRemove(e, treeId, treeNode) { //确定删除节点
    console.log('onRemove');
    console.log(treeNode)
}
function beforeRename(treeId, treeNode, newName, isCancel) {
    console.log('beforeRename');
    className = (className === "dark" ? "":"dark");
    if (newName.length == 0) {
        setTimeout(function() {
            var zTree = $.fn.zTree.getZTreeObj("treeDemo");
            zTree.cancelEditName();
            alert("节点名称不能为空。");
        }, 0);
        return false;
    }
    return true;
}
function onRename(e, treeId, treeNode, isCancel) {
    console.log('onRename')
    console.log(treeNode)
}
function showRemoveBtn(treeId, treeNode) {
    return true;
}
function showRenameBtn(treeId, treeNode) {
    return true;
}

var newCount = 1;
function addHoverDom(treeId, treeNode) {
    var sObj = $("#" + treeNode.tId + "_span");
    if (treeNode.editNameFlag || $("#addBtn_"+treeNode.tId).length>0) return;
    var addStr = "<span class='button add' id='addBtn_" + treeNode.tId
        + "' title='添加子节点' onfocus='this.blur();'></span>";
    sObj.after(addStr);
    $('.edit').attr('title', '编辑');
    $('.remove').attr('title', '删除');
    var btn = $("#addBtn_"+treeNode.tId);
    if (btn) btn.bind("click", function(){
        var zTree = $.fn.zTree.getZTreeObj("treeDemo");
        var obj = {id:(100 + newCount), pId:treeNode.id, name:"新增子节点 " + (newCount++)};
        zTree.addNodes(treeNode, obj);
        console.log(obj)
        console.log('添加成功')
        return false;
    });
};
function removeHoverDom(treeId, treeNode) {
    $("#addBtn_"+treeNode.tId).unbind().remove();
};

$(document).ready(function(){
    console.log('ready');
    $.fn.zTree.init($("#treeDemo"), setting, zNodes);
});